#!/usr/bin/perl 
use strict;

use DBI;
use CGI;
use TTraker;

initialize_dbi();
print_header();

my $form_ref = parse_input();

my %FORM_DATA = %$form_ref;
my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job_id = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $activity = $FORM_DATA{'empAct'};
my $session =  $FORM_DATA{'empSes'};	
my $doc = $FORM_DATA{'docNum'};
my $cli = $FORM_DATA{'cliId'};
my $dir = $FORM_DATA{'empDir'};
if ($dir = "") {
	$dir = "NA";
} 
my $msg = "";
if ( $cli eq "NUL"  ) {
	my @tmp = split(/\-/, $doc);
	if ($#tmp > 1) {
		$doc=$tmp[0];
		$cli=$tmp[1];
		$cli =~ tr/a-z/A-Z/;
		my $msg = get_session_message ( $num, $session);
		job_from_barcode ($num, $name, $job_id, $loc, $doc, $cli, $activity, $session, $msg);
	} else {
		no_job_created ($num, $name, $job_id, $loc, $doc, $cli, $session);
	}  
} else {
	make_client_active ($cli);
	create_new_job ($num, $doc, $cli, $session, $dir, $loc); 
	my $msg = get_session_message ( $num, $session);
	start_job_screen_from_pm  ($num, $name, $job_id, $loc, $activity, $session, $msg)
}

my $now = get_local_time($cli);
end_system_time ($num, "ALL", $now);
update_system_elapsed_time ($num );

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub job_from_barcode
{
my $num = $_[0];
my $name = $_[1];
my $job_id = $_[2];
my $loc = $_[3];
my $doc = $_[4];
my $cli = $_[5];
my $activity = $_[6];
my $session = $_[7];
my $msg = $_[8];

make_client_active ($cli);

# @clients = &get_clients;
print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_doc_num.html</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>

<div id='clock'></div>
<div id='box1'>

Employee Number:<br/><br/>
Employee Name:<br/><br/>
Document Number:<br/><br/>
Client ID:<br/><br/>
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_03_initiate_job.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job_id'/>
<input type="hidden" name=empLoc value='$loc'/>
<input type="hidden" name=docNum value='$doc'/>
<input type="hidden" name=cliId value='$cli'/>
<input type="hidden" name=empSes value='$session'/>

<span>$num</span><br/><br/>
<span>$name</span><br/><br/>
<span>$doc</span><br/><br/>
<span>$cli</span><br/><br/>
<input type='submit' value='Confirm Start Job'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}






sub emp_log_in_page
{
print <<ENDHEADER;
<html>
<head>
<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
<script type="text/javascript" src="${js_ref}trak.js"></script>
<title>trak_01_emp_num.html NO NUM</title>
</head>
<body onload="startTime()">
<h2><center>${banner}</center></h2>
<hr/>
<div id="clock"></div>
<div id="box1">
   &nbsp&nbsp&nbsp&nbsp&nbsp Employee ID: <br/><br/>
</div> 
<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_01_emp_log_in.pl" method="post">
   <input type="text" name=empID />&nbsp&nbsp
   <input type="submit" value="Log Into system">
</form>

</body>
</html>
ENDHEADER
}


sub no_job_created
{
my $num = $_[0];
my $name = $_[1];
my $job_id = $_[2];
my $loc = $_[3];
my $doc = $_[4];
my $cli = $_[5];
my $session = $_[6];

my $client_ref = get_clients(); 
my @clients = @$client_ref;
print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_doc_num.html</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>

<div id='clock'></div>
<div id='box1'>

Employee Number:<br/><br/>
Employee Name:<br/><br/>
Document Number:<br/><br/>
Client ID:<br/><br/>
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_03_initiate_job.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job_id'/>
<input type="hidden" name=empLoc value='$loc'/>
<input type="hidden" name=empAct value='$activity'/>
<input type="hidden" name=empSes value='$session'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="text" name="docNum" size=10 value="$doc" /><br/><br/>
<select id=clientDrop name="cliId">

ENDHEADER

# add a blank element at the start of the list
print "<option value='NUL'></option>";
# add all clients on the list to the drop down
foreach $cli (@clients) {
   print "<option value=$cli >$cli</option>";
}

print <<ENDHEADER;

</select>
<br/><br/>
<input type='submit' value='Start New Job'>
<br/><br/>
<b id="warn"> NO JOB CREATED Require Client Id!</b>
<br/><br/> 
<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
</form>
</body>
</html>

ENDHEADER
}


